package com.itheima.reggie.controller;

import com.itheima.reggie.commons.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags ="购物车")
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController extends BaseController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        Long userId = getLoginIdFromSession();
        shoppingCart.setUserId(userId);
        ShoppingCart cart = shoppingCartService.add(shoppingCart);
        log.info("添加成功");
        return R.success(cart);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        Long userId = getLoginIdFromSession();
        shoppingCart.setUserId(userId);
        ShoppingCart cart = shoppingCartService.subNumber(shoppingCart);
        log.info("减少数量成功");
        return R.success(cart);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        Long userId = getLoginIdFromSession();
        List<ShoppingCart> cartList = shoppingCartService.findCartItemByUserId(userId);
        return R.success(cartList);
    }

    @DeleteMapping("/clean")
    public R<String> deleteByuserId() {
        Long userId = getLoginIdFromSession();
        shoppingCartService.deleteCartItemByUserId(userId);
        return R.success("删除成功");
    }

}
